//
//  DynamicThreeCollectionViewCell.m
//  MJApp
//
//  Created by wangliang on 15/5/27.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "DynamicThreeCollectionViewCell.h"
#import "DynamicPicCollectionViewCell.h"
#import "PhotoBroswerVC.h"

@interface DynamicThreeCollectionViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
@property (weak, nonatomic) IBOutlet UILabel *readLabel;
@property (weak, nonatomic) IBOutlet UIButton *acountBtn;
@property (weak, nonatomic) IBOutlet UIButton *moreBtn;
@property (weak, nonatomic) IBOutlet UILabel *descLable;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *timeLable;

@end

@implementation DynamicThreeCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    
    [self UI];

}

- (void)setInfoDic:(NSDictionary *)infoDic {
    
    _infoDic = infoDic;
    self.nameLable.text = infoDic[@"nick_name"];
    self.timeLable.text = infoDic[@"create_time"];
    self.descLable.text = infoDic[@"trends"];
    
    NSInteger rainNum = [infoDic[@"discuss"] count];
    NSString *rainStr = [NSString stringWithFormat:@"     %ld评论     ",(long)rainNum];
    [self.acountBtn setTitle:rainStr forState:UIControlStateNormal];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kUrl,infoDic[@"head"]]];
    [self.logoImageView sd_setImageWithURL:url];
    if ([infoDic[@"images"] count] > 0) {
        
        self.collectionView.hidden = NO;
        [self.collectionView reloadData];
    }else {
        
        self.collectionView.hidden = YES;
    }
}


- (void)UI {
    
    self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.layer.borderWidth = 0.5;
    
    self.readLabel.text = @"243\n阅读";
    self.readLabel.layer.borderWidth = 0.5;
    self.readLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.readLabel.layer.cornerRadius = 3;
    
    self.logoImageView.layer.cornerRadius = CGRectGetHeight(self.logoImageView.frame) / 2;
    self.logoImageView.clipsToBounds = YES;
    
    self.acountBtn.layer.borderColor = [UIColor orangeColor].CGColor;
    self.acountBtn.layer.borderWidth = 0.5;
    self.acountBtn.layer.cornerRadius = CGRectGetHeight(self.acountBtn.frame) / 2;
    [self.acountBtn setTitle:@"     321评论     " forState:UIControlStateNormal];
    
    self.moreBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.moreBtn.layer.borderWidth = 0.5;
    self.moreBtn.layer.cornerRadius = CGRectGetHeight(self.acountBtn.frame) / 2;
    
    self.descLable.text = @"为科技开发";
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 30;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.itemSize = CGSizeMake(80, 80);
    
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    UINib *nib = [UINib nibWithNibName:@"DynamicPicCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"DynamicPicCollectionViewCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (self.infoDic == nil) {
        
        return 0;
    }else {
        
        return [self.infoDic[@"images"] count];
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DynamicPicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DynamicPicCollectionViewCell" forIndexPath:indexPath];
    cell.infoDic = self.infoDic[@"images"][indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  
    DynamicPicCollectionViewCell *cell = (DynamicPicCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    [PhotoBroswerVC show:(UIViewController *)self.delegate type:PhotoBroswerVCTypeZoom index:indexPath.row photoModelBlock:^NSArray *{
        
        
        NSMutableArray *networkImages = [NSMutableArray array];
        for (NSDictionary *str in self.infoDic[@"images"]) {
            
            NSString *urlStr = [NSString stringWithFormat:@"%@%@",kUrl,str[@"image"]];
            [networkImages addObject:urlStr];
        }
        NSMutableArray *modelsM = [NSMutableArray arrayWithCapacity:networkImages.count];
        for (NSUInteger i = 0; i< networkImages.count; i++) {
            
            PhotoModel *pbModel=[[PhotoModel alloc] init];
            pbModel.mid = i + 1;
            pbModel.image_HD_U = networkImages[i];
            //源frame
            UIImageView *imageV = cell.picImageView;
            pbModel.sourceImageView = imageV;
            [modelsM addObject:pbModel];
        }
        
        return modelsM;
    }];
}

- (IBAction)moreBtn:(id)sender {

    UIButton *btn = sender;
    if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
        
        NSDictionary *dic = @{@"index":@(btn.tag)};
        [self.delegate homeCell:self withInfo:dic];
    }
}
- (IBAction)acountBtn:(id)sender {
    
    UIButton *btn = sender;
    if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
        
        NSDictionary *dic = @{@"index":@(btn.tag)};
        [self.delegate homeCell:self withInfo:dic];
    }
}

@end
